home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 10868 / 10868.xpi / modules / stores.js < prev    next >
Encoding:
Text File  |  2010-02-02  |  5.0 KB  |  190 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Dan Mills <thunder@mozilla.com>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ["Store"];
  38.  
  39. const Cc = Components.classes;
  40. const Ci = Components.interfaces;
  41. const Cr = Components.results;
  42. const Cu = Components.utils;
  43.  
  44. Cu.import("resource://weave/log4moz.js");
  45. Cu.import("resource://weave/constants.js");
  46. Cu.import("resource://weave/util.js");
  47.  
  48. /*
  49.  * Data Stores
  50.  * These can wrap, serialize items and apply commands
  51.  */
  52.  
  53. function Store() {
  54.   this._init();
  55. }
  56. Store.prototype = {
  57.   _logName: "BaseClass",
  58.  
  59.   // set this property in child object's wrap()!
  60.   _lookup: null,
  61.  
  62.   get cache() {
  63.     let cache = new Cache();
  64.     this.__defineGetter__("cache", function() cache);
  65.     return cache;
  66.   },
  67.  
  68.   _init: function Store__init() {
  69.     this._log = Log4Moz.repository.getLogger("Store." + this._logName);
  70.     let level = Svc.Prefs.get("log.logger.engine." + this.name, "Debug");
  71.     this._log.level = Log4Moz.Level[level];
  72.   },
  73.  
  74.   applyIncoming: function Store_applyIncoming(record) {
  75.     if (record.deleted)
  76.       this.remove(record);
  77.     else if (!this.itemExists(record.id))
  78.       this.create(record);
  79.     else
  80.       this.update(record);
  81.   },
  82.  
  83.   // override these in derived objects
  84.  
  85.   create: function Store_create(record) {
  86.     throw "override create in a subclass";
  87.   },
  88.  
  89.   remove: function Store_remove(record) {
  90.     throw "override remove in a subclass";
  91.   },
  92.  
  93.   update: function Store_update(record) {
  94.     throw "override update in a subclass";
  95.   },
  96.  
  97.   itemExists: function Store_itemExists(id) {
  98.     throw "override itemExists in a subclass";
  99.   },
  100.  
  101.   createRecord: function Store_createRecord(id) {
  102.     throw "override createRecord in a subclass";
  103.   },
  104.  
  105.   changeItemID: function Store_changeItemID(oldID, newID) {
  106.     throw "override changeItemID in a subclass";
  107.   },
  108.  
  109.   getAllIDs: function Store_getAllIDs() {
  110.     throw "override getAllIDs in a subclass";
  111.   },
  112.  
  113.   wipe: function Store_wipe() {
  114.     throw "override wipe in a subclass";
  115.   }
  116. };
  117.  
  118. function Cache() {
  119.   this.count = 0;
  120.   this.maxItems = 100;
  121.   this.fifo = true;
  122.   this.enabled = true;
  123.   this._head = this._tail = null;
  124.   this._items = {};
  125. }
  126. Cache.prototype = {
  127.   remove: function Cache_remove(item) {
  128.     if (this.count <= 0 || this.count == 1) {
  129.       this.clear();
  130.       return;
  131.     }
  132.  
  133.     item.next.prev = item.prev;
  134.     item.prev.next = item.next;
  135.  
  136.     if (item == this._head)
  137.       this._head = item.next;
  138.     if (item == this._tail)
  139.       this._tail = item.prev;
  140.  
  141.     item.next = null;
  142.     item.prev = null;
  143.  
  144.     delete this._items[item.id];
  145.     this.count--;
  146.   },
  147.   pop: function Cache_pop() {
  148.     if (this.fifo)
  149.       this.remove(this._tail);
  150.     else
  151.       this.remove(this._head);
  152.   },
  153.   put: function Cache_put(id, item) {
  154.     if (!this.enabled)
  155.       return;
  156.  
  157.     let wrapper = {id: id, item: item};
  158.  
  159.     if (this._head === null) {
  160.       wrapper.next = wrapper;
  161.       wrapper.prev = wrapper;
  162.       this._head = wrapper;
  163.       this._tail = wrapper;
  164.     } else {
  165.       wrapper.next = this._tail;
  166.       wrapper.prev = this._head;
  167.       this._head.prev = wrapper;
  168.       this._tail.next = wrapper;
  169.       this._tail = wrapper;
  170.     }
  171.  
  172.     this._items[wrapper.id] = wrapper;
  173.     this.count++;
  174.  
  175.     if (this.count >= this.maxItems)
  176.       this.pop();
  177.   },
  178.   get: function Cache_get(id) {
  179.     if (id in this._items)
  180.       return this._items[id].item;
  181.     return undefined;
  182.   },
  183.   clear: function Cache_clear() {
  184.     this.count = 0;
  185.     this._head = null;
  186.     this._tail = null;
  187.     this._items = {};
  188.   }
  189. };
  190.